package com.taotao.admin.rest;

import com.rop.client.CompositeResponse;
import com.taotao.admin.MyRopClient;
import com.taotao.admin.pojo.TableUI;
import com.taotao.rop.database.domain.OtherTel;
import com.taotao.rop.database.domain.Tel;
import com.taotao.rop.response.BaseResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.LinkedHashMap;
import java.util.List;

@Controller
@RequestMapping(value = "/otherTel")
public class OtherTelController {
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public Object getListByPage(HttpServletRequest request) throws UnsupportedEncodingException {
        request.setCharacterEncoding("UTF-8");
        Long pageSize = Long.parseLong(request.getParameter("pageSize"));
        Long pageNo = Long.parseLong(request.getParameter("pageNo"));

        OtherTel condition = new OtherTel();
        condition.setPageSize(pageSize);
        condition.setPageNo(pageNo);

        CompositeResponse<BaseResponse> response = MyRopClient.getRopClient(request.getHeader("sessionId")).buildClientRequest()
                .post(condition, BaseResponse.class, "otherTel.listByPage", "1.0");
        if (response.isSuccessful()){
            BaseResponse resp = response.getSuccessResponse();
            if (resp.getSuccess()){
                TableUI tableUI = new TableUI();
                List<LinkedHashMap<String, Object>> respData = (List<LinkedHashMap<String, Object>>) resp.getData();
                tableUI.setTotal(resp.getMsg());
                tableUI.setRows(respData);
                return tableUI;
            } else {
                return resp;
            }
        } else {
            return response.getErrorResponse().getSubErrors().get(0);
        }
    }

    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public Object create(HttpServletRequest request) throws UnsupportedEncodingException {
        request.setCharacterEncoding("UTF-8");
        Tel tel = null;
        try {
            tel = getTelFromRequest(request);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (tel != null) {
            CompositeResponse<BaseResponse> response = MyRopClient.getRopClient(request.getHeader("sessionId")).buildClientRequest()
                    .post(tel, BaseResponse.class, "tel.insert", "1.0");
            if (response.isSuccessful()){
                //添加成功的，从othertel中删除
                MyRopClient.getRopClient(request.getHeader("sessionId")).buildClientRequest()
                        .post(tel, BaseResponse.class, "otherTel.delete", "1.0");
                return response.getSuccessResponse();
            } else {
                return response.getErrorResponse().getSubErrors().get(0);
            }
        } else {
            BaseResponse resp = new BaseResponse();
            resp.setMsg("输入有误，请核对");
            return resp;
        }
    }

    @RequestMapping(value = "/delete/{noSegment}", method = RequestMethod.POST)
    @ResponseBody
    public Object delete(HttpServletRequest request, @PathVariable String noSegment) throws UnsupportedEncodingException {
        request.setCharacterEncoding("UTF-8");
        OtherTel otherTel = new OtherTel();
        otherTel.setNoSegment(Integer.parseInt(noSegment));
        CompositeResponse<BaseResponse> response = MyRopClient.getRopClient(request.getHeader("sessionId")).buildClientRequest()
                .post(otherTel, BaseResponse.class, "otherTel.delete", "1.0");
        if (response.isSuccessful()){
            return response.getSuccessResponse();
        } else {
            return response.getErrorResponse().getSubErrors().get(0);
        }
    }

    private Tel getTelFromRequest(HttpServletRequest request) throws Exception {
        String numType = request.getParameter("numType");
        String areaNum = request.getParameter("areaNum");
        String area = request.getParameter("area");
        String noSegment = request.getParameter("noSegment");

        Tel tel = new Tel();
        tel.setNoSegment(Integer.parseInt(noSegment));
        tel.setArea(area);
        tel.setAreaNum(areaNum);
        tel.setNumType(numType);
        return tel;
    }
}
